package com.xoa.controller.secureKey;

import com.xoa.model.secureKey.SecureKey;
import com.xoa.service.secureKey.SecureKeyService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.dataSource.ContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by 张丽军 on 2019/7/26.
 */
@Controller
@RequestMapping("/secureKey")
public class SecureKeyController {
    @Resource
    SecureKeyService secureKeyService;


    //导入卡信息页面
    @RequestMapping("/insertImportCard")
    public String insertImportCard(HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        return "app/user/insertImportCard";
    }

    //批量绑定页面
    @RequestMapping("/bulkBinding")
    public String bulkBinding(HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        return "app/user/bulkBinding";
    }


    /**
     * 导入动态密码卡信息
     * 张丽军
     * 2019-7-26
     * @param request
     * @param response
     * @param file
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/ImportSecureKeyInfo", produces = {"application/json;charset=UTF-8"})
    public ToJson<SecureKey> ImportSecureKeyInfo(HttpServletRequest request, HttpServletResponse response,MultipartFile file) {
        return secureKeyService.ImportSecureKeyInfo(request, response,file);
    }


    /**
     * 动态密码卡管理、查询所有密码卡信息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/SelectSecureKeyInfo", produces = {"application/json;charset=UTF-8"})
    public ToJson<SecureKey> SelectSecureKeyInfo(HttpServletRequest request, Integer page, Integer pageSize, boolean useFlag) {
        return secureKeyService.SelectSecureKeyInfo(request,page,pageSize,useFlag);
    }

    /**
     * 删除动态密码卡
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteSecureKeyInfo", produces = {"application/json;charset=UTF-8"})
    public ToJson<SecureKey> deleteSecureKeyInfo(HttpServletRequest request,String keySn) {
        return secureKeyService.deleteSecureKeyInfo(request,keySn);
    }

    /**
     * 同步用户动态密码卡信息
     * 张丽军
     * keySn 卡号  dynPassword 动态密码
     * 2019-7-29
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/user/passwordsyn",produces = {"application/json;charset=UTF-8"})
    public ToJson<SecureKey> passwordsyn(HttpServletRequest request, String keySn, String dynPassword){
        return secureKeyService.passwordsyn(request,keySn,dynPassword);
    }
}
